package com.bilibili.api;

import com.bilibili.api.support.UserSupport;
import com.bilibili.domain.JsonResponse;
import com.bilibili.domain.PageResult;
import com.bilibili.domain.entity.*;
import com.bilibili.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.mahout.cf.taste.common.TasteException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * projectName:com.bilibili.api
 *
 * @author 飞
 * @create by 2024/6/1622:08
 * description:
 */
@RestController
@Api(tags = "视频接口")
public class VideoController {

    @Resource
    private UserSupport userSupport;

    @Resource
    private VideoService videoService;

    @Resource
    private VideoLikeService videoLikeService;

    @Resource
    private VideoCollectionService videoCollectionService;

    @Resource
    private VideoCoinService videoCoinService;

    @Resource
    private VideoCommentService videoCommentService;

    @Resource
    private ElasticSearchService elasticSearchService;

    @PostMapping("/videos")
    @ApiOperation("添加视频")
    public JsonResponse<String> addVideos(@RequestBody Video video){
        Long userId = userSupport.getCurrentUserId();
        video.setUserId(userId);
        videoService.addVideos(video);
        //在es中添加一条视频
        elasticSearchService.addVideo(video);
        return JsonResponse.success();
    }

    @GetMapping("/videos")
    @ApiOperation("分页查询")
    public JsonResponse<PageResult<Video>> pageListVideos(Integer size,Integer no,String area){
        PageResult<Video> result = videoService.pageListVideos(size,no,area);
        return new JsonResponse<>(result);
    }

    @GetMapping("/video-slices")
    @ApiOperation("在线观看")
    public void viewVideoOnlineBySlices(HttpServletRequest request,
                                        HttpServletResponse response,
                                        String url) throws Exception {
        videoService.viewVideoOnlineBySlices(request,response,url);
    }

    @PostMapping("/video-likes")
    @ApiOperation("视频点赞")
    public JsonResponse<String> addVideoLike(@RequestParam Long videoId){
        Long userId = userSupport.getCurrentUserId();
        videoLikeService.addVideoLike(videoId,userId);
        return JsonResponse.success();
    }

    @DeleteMapping("/video-likes")
    @ApiOperation("取消点赞")
    public JsonResponse<String> deleteVideoLike(@RequestParam Long videoId){
        Long userId = userSupport.getCurrentUserId();
        videoLikeService.deleteVideoLik(videoId,userId);
        return JsonResponse.success();
    }

    @GetMapping("/video-likes")
    @ApiOperation("查询视频点赞数量")
    public JsonResponse<Map<String,Object>> getVideoLike(@RequestParam Long videoId){
        Long userId = null;//游客模式

        //空则会抛出异常
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {}
        Map<String,Object> result = videoLikeService.getVideoLike(videoId,userId);
        return new JsonResponse<>(result);
    }


    @PostMapping("/video-collection")
    @ApiOperation("视频收藏")
    public JsonResponse<String> addVideoCollection(@RequestBody VideoCollection videoCollection){
        Long userId = userSupport.getCurrentUserId();
        videoCollectionService.addVideoCollection(videoCollection,userId);
        return JsonResponse.success();
    }

    @DeleteMapping("/video-collection")
    @ApiOperation("取消收藏")
    public JsonResponse<String> deleteVideoCollection(@RequestParam Long videoId){
        Long userId = userSupport.getCurrentUserId();
        videoCollectionService.deleteVideoCollection(videoId,userId);
        return JsonResponse.success();
    }

    @GetMapping("/video-collection")
    @ApiOperation("查询视频收藏数量")
    public JsonResponse<Map<String,Object>> getVideoCollection(@RequestParam Long videoId){
        Long userId = null;//游客模式

        //空则会抛出异常
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {}
        Map<String,Object> result = videoCollectionService.getVideoCollection(videoId,userId);
        return new JsonResponse<>(result);
    }



    @PostMapping("/video-coins")
    @ApiOperation("视频投币")
    public JsonResponse<String> addVideoCoins(@RequestBody VideoCoin videoCoin){
        Long userId = userSupport.getCurrentUserId();
        videoCoinService.addVideoCoins(videoCoin,userId);
        return JsonResponse.success();
    }

    @GetMapping("/video-coins")
    @ApiOperation("查询视频投币数量")
    public JsonResponse<Map<String,Object>> getVideoCoins(@RequestParam Long videoId){
        Long userId = null;//游客模式

        //空则会抛出异常
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {}
        Map<String,Object> result = videoCoinService.getVideoCoins(videoId,userId);
        return new JsonResponse<>(result);
    }


    @PostMapping("/video-comments")
    @ApiOperation("视频评论")
    public JsonResponse<String> addVideoComment(@RequestBody VideoComment videoComment){
        Long userId = userSupport.getCurrentUserId();
        videoCommentService.addVideoComment(videoComment,userId);
        return JsonResponse.success();
    }

    @GetMapping("/video-comments")
    @ApiOperation("评论分页查询")
    public JsonResponse<PageResult<VideoComment>> pageListVideoComments(@RequestParam Integer size,
                                                                        @RequestParam Integer no,
                                                                        @RequestParam Long videoId){
        PageResult<VideoComment> result = videoCommentService.pageListVideoComments(size,no,videoId);
        return new JsonResponse<>(result);
    }

    @GetMapping("/video-details")
    @ApiOperation("获取视频详情")
    public JsonResponse<Map<String,Object>> getVideoDetails(Long videoId){
        Map<String,Object> result = videoService.getVideoDetails(videoId);
        return new JsonResponse<>(result);
    }

    @ApiOperation("添加视频观看记录")
    @PostMapping("/video-views")
    public JsonResponse<String> addVideoView(@RequestBody VideoView videoView,
                                             HttpServletRequest request){
        Long userId;
        try{
            userId = userSupport.getCurrentUserId();
            videoView.setUserId(userId);
            videoService.addVideoView(videoView, request);
        }catch (Exception e){
            videoService.addVideoView(videoView, request);
        }
        //同步更新视频播放量到Elasticsearch
        elasticSearchService.updateVideoViewCount(videoView.getVideoId());
        return JsonResponse.success();
    }

    @ApiOperation("查询视频播放量")
    @GetMapping("/video-view-counts")
    public JsonResponse<Integer> getVideoViewCounts(@RequestParam Long videoId){
        Integer count = videoService.getVideoViewCounts(videoId);
        return new JsonResponse<>(count);
    }

    @ApiOperation("视频内容推荐")
    @GetMapping("/recommendations")
    public JsonResponse<List<Video>> recommend() throws TasteException {
        Long userId = userSupport.getCurrentUserId();
        List<Video> list = videoService.recommend(userId);
        return new JsonResponse<>(list);
    }
}
